// pages/tjfd/tjfd.
const app = getApp();
const sendMessage = require('../home/home.js')
const util = require('../../utils/util')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    second:180,
    times:0,
    img:["/images/up.png","/images/right.png","/images/bottom.png","/images/left.png"],
    list:[],
    selected:"",
    visualSize:'',
    trainType:'',
    eyeType:'',
    correct:0,
    error:0,
    timer:'',
    currentIndex:'',
    contentList:[],
    page:0,
    inter:'',
    grade:0,
    equipmentNumber:''
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var that = this;
    var visualSize = options.visualSize;
    var trainType = options.trainType;
    var eyeType = options.eyeType;
    if (trainType == '常规'){
      var list = [];
      for(var i=0;i<3;i++){
        let random = Math.floor(Math.random() * 4);
        var selected = false;
        if(i == 0){
          selected = true;
        }
        list.push({"random":random,"selected":selected,"num":i});
      }
      that.setData({
        list:list,
        selected:0,
        visualSize:visualSize,
        trainType:trainType,
        eyeType:eyeType
      });
    } else {
      that.startInter();
      wx.request({
        //url: 'http://124.71.238.22:8099/content/list', 
        url:'https://pnst.tsintell.com/content/list',
        method: 'get',
        data:{
          page:that.data.page
        },
        header: {
          'Content-Type': 'application/x-www-form-urlencoded',
        },
        success: function (res) {//这里写调用接口成功之后所运行的函数
          if(res.data.code == 200){
            var list =[];
            list.push(res.data.contentDataDOList);
            that.setData({
              contentList:list,
              trainType:trainType,
            })
          }
        },
        fail: function (res) {//这里写调用接口失败之后所运行的函数
        }
      })
    }
    that.startCountdown();
  },

/**点击 */
dianji(e){
  var that = this;
  var type = e.target.dataset.type;
  var selected = that.data.selected;
  var list = that.data.list;
  var corrects = that.data.correct;
    if(type == list[selected].random){
      list[selected].selected = false;
      if(selected+1 < list.length){
        console.log("1213");
        selected += 1;
        list[selected].selected = true;
      }else{
        list = [];
        console.log("4546");
        for(var i=0;i<3;i++){
          let random = Math.floor(Math.random() * 4);
          var selected = false;
          // if (this.data.eyeType =='右眼'){
          //   if(i == 2){
          //     selected = true;
          //   }
          // } else {
          //   if(i == 0){
          //     selected = true;
          //   }
          // }
          if(i == 0){
            selected = true;
          }
          list.push({"random":random,"selected":selected,"num":i});
        }
        selected = 0;
        list[0].selected = true;
      }
      console.log("正确 "+"selected："+selected);
      sendMessage.BLEDataSend("aa55a55a210100cc66c66c");
      corrects +=1;
      that.setData({
        list:list,
        selected:selected,
        correct:corrects
      });
      that.end();
      that.start();
    }else{
      if(type == 4){
        console.log("看不清 "+"selected："+selected);
        list[selected].selected = false;
        if(selected+1 < list.length){
          selected += 1;
          list[selected].selected = true;
        }else{
          selected = 0;
          list[0].selected = true;
        }
        that.setData({
          list:list,
          selected:selected
        });
        that.end();
        that.start();
      }else{
        var errors = that.data.error;
        errors +=1;
        var grade = that.data.grade;
        grade = grade +1;
        that.setData({
          error:errors,
          grade:grade
        })
        wx.showToast({
          title: "错误",
          icon: "none",
          mask: false, 
          duration:1000 
        });
      }
    }
 
},
start(){
  var that = this;
  var time =  that.data.time;
  // var timer = setInterval(()=>{
  //      time += 0.01;
  //      that.setData({
  //       time:time.toFixed(2),
  //      })
  // },10);
  // that.setData({
  //   timer:timer,
  // });
},
end(){
  var that = this;
  console.log("结束：");
  that.setData({
    time:0,
  });
 // clearInterval(that.data.timer);
},
startCountdown() {
  var openId= wx.getStorageSync('openid');
  var that = this;
  var second = that.data.second;
  var grade = that.data.grade;
  var equipmentNumber = app.nowEquipmentId;
  var score = 0;
  if (second == 0) {
    clearInterval(that.data.timer);
    clearInterval(that.data.inter);
    if (grade <=3){
      score = 5;
    } else if (grade <=6 && grade >3){
      score = 4;
    } else if (grade <=9 && grade >6){
      score = 3;
    } else {
      score = 2;
    }
    wx.navigateTo({
      url: '../fraction/fraction?fraction='+score,
    })
  }
  that.data.timer = setTimeout(function () {
    that.setData({
      second: second - 1
    });
    that.startCountdown();
  } , 1000)
  var times = that.data.times;
  times++;
  that.setData({
    times:times,
    equipmentNumber:equipmentNumber
  })
},
/**上一页 */
upPage(e){
  var that = this;
  var page = e.target.dataset.page;
  if (page == 0){
    wx.showToast({
      title: '已经是第一页了',
      icon: 'none',
      duration: 2000
    })
    return;
  } else{
    page = page -1;
    that.setData({
      page:page
    })
    util.pageInfo(that)
  }
},
/**下一页 */
nextPage(e){
  var that = this;
  var page = e.target.dataset.page;
  page = page +1;
  that.setData({
    page:page
  })
  util.pageInfo(that)
},
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    console.log("324234");
    var that = this;
    var visualSize = that.data.visualSize;
    var trainType = that.data.trainType;
    var eyeType = that.data.eyeType;
    var list = [];
      for(var i=0;i<3;i++){
        let random = Math.floor(Math.random() * 4);
        var selected = false;
        // if (eyeType =='右眼'){
        //   if(i == 2){
        //     selected = true;
        //   }
        // } else {
        //   if(i == 0){
        //     selected = true;
        //   }
        // }
        if(i == 0){
          selected = true;
        }
        list.push({"random":random,"selected":selected,"num":i});
      }
      that.setData({
        list:list,
        selected:0,
        visualSize:visualSize,
        trainType:trainType,
        eyeType:eyeType
      });
  },
  startInter: function(){
    var that = this;
    that.data.inter= setInterval(function () {
      sendMessage.BLEDataSend("aa55a55a210100cc66c66c");
      console.log("定时任务开启");
    }, 10000)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {
    console.log("定时任务销毁");
    var openId= wx.getStorageSync('openid');
    clearInterval(this.data.timer);
    clearInterval(this.data.inter);
    this.setData({
      trainName:"调节幅度",
      openId:openId,
      times:180
    })
    util.saveRecord(this);
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
    var second = this.data.second;
    if (second ==0){
      return;
    }
    var openId= wx.getStorageSync('openid');
    console.log("定时任务卸载");
    clearInterval(this.data.timer);
    clearInterval(this.data.inter);
    this.setData({
      trainName:"调节幅度",
      openId:openId
    })
    util.saveRecord(this);
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})
